package com.itheima.ydd.service.impl;

import com.baomidou.mybatisplus.core.conditions.Wrapper;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.itheima.ydd.common.R;
import com.itheima.ydd.dto.AdvimgDto;
import com.itheima.ydd.dto.PageInfo;
import com.itheima.ydd.mapper.YddAdvimgMapper;
import com.itheima.ydd.pojo.YddAdvimg;
import com.itheima.ydd.pojo.YddAdvpos;
import com.itheima.ydd.service.YddAdvimgService;
import com.itheima.ydd.service.YddAdvposService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import java.util.List;
import java.util.stream.Collectors;

@Service
public class YddAdvimgServiceImpl extends ServiceImpl<YddAdvimgMapper, YddAdvimg> implements YddAdvimgService {
    @Autowired
    private YddAdvposService yddAdvposService;

    /**
     * 根据id查询广告图详情
     *
     * @param id
     * @return
     */
    @Override
    public ResponseEntity<R<YddAdvimg>> getAdvById(Integer id) {
        YddAdvimg byId = getById(id);
        return ResponseEntity.ok(R.success(byId));
    }

    /**
     * 广告图列表分页查询,需要根据advimgpos 来查询到广告位位置name
     *
     * @param advimgdesc
     * @param advimgpos
     * @param page
     * @param limit
     * @return
     */
    @Override
    public ResponseEntity<R<PageInfo<AdvimgDto>>> listByPagesWithPosName(String advimgdesc, String advimgpos, Integer page, Integer limit) {
        //根据广告位描述模糊查询advimgdesc 根据 advimgpos查询
        IPage<YddAdvimg> pageImg = new Page<>(page, limit);
        LambdaQueryWrapper<YddAdvimg> wrapperImg = new LambdaQueryWrapper<>();
        wrapperImg.like(StringUtils.hasText(advimgdesc), YddAdvimg::getAdvimgdesc, advimgdesc)
                .eq(StringUtils.hasText(advimgpos), YddAdvimg::getAdvimgpos, advimgpos);
        page(pageImg, wrapperImg);
        //此时pageImg对象已经存储了广告图的数据,需要根据pos查询出advposname
        //针对records单独处理
        List<YddAdvimg> records = pageImg.getRecords();
        List<AdvimgDto> records4dto = records.stream().map((item) -> {
            String advimgposId = item.getAdvimgpos();
            //根据这个查询出广告位名称
            YddAdvpos byId = yddAdvposService.getById(advimgposId);
            String advposname = byId.getAdvposname();
            AdvimgDto advimgDto = new AdvimgDto();
            //复制
            BeanUtils.copyProperties(item, advimgDto);
            advimgDto.setAdvposname(advposname);
            return advimgDto;

        }).collect(Collectors.toList());
        //自定义的pageinfo对象
        PageInfo<AdvimgDto> pageInfo=new PageInfo<>();
        pageInfo.setData(records4dto);
        pageInfo.setCount((int) pageImg.getTotal());
        pageInfo.setTotalPages((int) pageImg.getPages());
        pageInfo.setCurrentPage((int) pageImg.getCurrent());
        pageInfo.setPageSize((int) pageImg.getSize());

        return ResponseEntity.ok(R.success(pageInfo));

    }

    /**
     * 广告图修改
     *
     * @param yddAdvimg
     * @return
     */
    @Override
    public ResponseEntity<R<String>> updateImg(YddAdvimg yddAdvimg) {
        updateById(yddAdvimg);
        return ResponseEntity.ok(R.success("修改成功"));
    }


}
